package main;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * Klasa Panelu logowania
 * @author Maciej Krawczyk
 */
@SuppressWarnings("serial")
public class InitiationPanel extends JPanel implements KeyListener, ActionListener {

	/**Ramka logowania*/
	private InitiationFrame initiationFrame;
	/**Etykiety odpowiadajace za napisy "Port", "Host" oraz etykieta z komunikatem*/
	private JLabel hostLabel, portLabel, promptLabel;
	/**Pola tekstowe na host i port*/
	private JTextField hostField, portField;
	/**Przycisk polaczenia*/
	private JButton connectionButton;
	/**Czcionka*/
	private Font font;
	/**Modyfikator zarzadcy rozkladem*/
	private GridBagConstraints c;
	/**Gniazdko sieciowe, na ktorym oparte jest polaczenie z serwerem*/
	private Socket socket;
	/**Strumien wyjscia*/
	private PrintWriter out;
	/**Strumien wejscia*/
	private BufferedReader in;
	
	/**
	 * Konstruktor panelu logowania
	 * @param frame ramka logowania
	 */
	public InitiationPanel(InitiationFrame frame) {
		this.initiationFrame = frame;
		this.setLayout(new GridBagLayout());	
		font = new Font(Font.SERIF,Font.PLAIN,30);
		c = new GridBagConstraints();
		c.insets = new Insets(10, 10, 5, 5);
		
		hostLabel = new JLabel("Host:");
		hostLabel.setFont(font);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 0;
		this.add(hostLabel, c);
		
		hostField = new JTextField(9);
		hostField.setFont(font);
		hostField.addKeyListener(this);
		c.gridx = 1;
		this.add(hostField, c);
		

		portLabel = new JLabel("Port:");
		portLabel.setFont(font);
		portLabel.setHorizontalAlignment(JLabel.RIGHT);
		c.gridx = 0;
		c.gridy = 1;
		this.add(portLabel, c);
		
		portField = new JTextField(9);
		portField.setFont(font);
		portField.addKeyListener(this);
		c.gridx = 1;
		this.add(portField, c);
		
		connectionButton = new JButton("Connect");		
		connectionButton.setFont(font);
		connectionButton.addActionListener(this);
		connectionButton.setFocusable(false);
		c.gridx = 0;
		c.gridy = 2;
		c.gridwidth = 2;
		this.add(connectionButton, c);
		
		promptLabel = new JLabel("Please enter the data.");
		promptLabel.setFont(font);
		promptLabel.setForeground(new Color(206, 26, 44));
		promptLabel.setBackground(Color.WHITE);
		promptLabel.setOpaque(true);
		c.gridy = 3;
		this.add(promptLabel, c);
	}

	/**Metoda probuje nawiazac polaczenie z serwerem po wcisnieciu przycisku*/
	@Override
	public void actionPerformed(ActionEvent e) {
		String host = hostField.getText();
		int port;
		try {
			port = Integer.parseInt(portField.getText());
		} catch(NumberFormatException ex) {
			portField.setText("");
			promptLabel.setText("Wrong port type!");
			return;
		}
		connecting(host, port);
	}

	/**Metoda probuje nawiazac polaczenie z serwerem po wcisnieciu klawisza Enter*/
	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == 10) {
			String host = hostField.getText();
			int port;
			try {
				port = Integer.parseInt(portField.getText());
			} catch(NumberFormatException ex) {
				portField.setText("");
				promptLabel.setText("Wrong port type!");
				return;
			}
			connecting(host, port);
		}			
	}

	/**Nieuzywane*/
	@Override
	public void keyReleased(KeyEvent e) {}

	/**Nieuzywane*/
	@Override
	public void keyTyped(KeyEvent e) {}
	
	/**Metoda nawiazuje polaczenie na wprowadzonym przez gracza porcie i hoscie*/
	public void connecting(String host, int port) {
		
		try {
			socket = new Socket("localhost", 44);
			out = new PrintWriter(socket.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		} catch (IOException e) {
			hostField.setText("");
			portField.setText("");
			promptLabel.setText("Please re-enter the data!");
			return;
		}
		
		hostField.setEditable(false);
		portField.setEditable(false);
		hostField.setEnabled(false);
		portField.setEnabled(false);
		connectionButton.setEnabled(false);
		promptLabel.setText("Connected, please wait!");
		initiationFrame.pack();
		
		new GameFrame(socket, out, in, initiationFrame);
	}
}
